package com.jeeplus.common.utils;

import com.aliyun.oss.OSS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;

@Service
public class FileStorageService {

    @Autowired
    private OssConfig ossConfig;

    @Autowired
    private OSS ossClient;

    /**
     * 上传图片
     * @param objectName
     * @param is
     */
    public void save(String objectName, InputStream is) {
        try {
            ossClient.putObject(ossConfig.getBucket(), objectName, is);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断文件是否存在
     * @param objectName
     * @return
     */
    public boolean isExit(String objectName) {
        try {
            boolean found = ossClient.doesObjectExist(ossConfig.getBucket(), objectName);
            return found;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除文件
     * @param objectName
     * @return
     */
    public boolean delete(String objectName) {
        try {
            ossClient.deleteObject(ossConfig.getBucket(), objectName);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
